package com.wm.blog_common.bean;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_common.base.BeanUtils;

import java.util.List;

/***
 * @ClassName: PageHelper
 * @Description:
 * @Author: wm_yu
 * @Create_time: 11:10 2020-3-4
 */
public class PageHelper {
    private PageHelper() {
    }

    public static <T> Page<T> newPage(IPage<T> rawPage) {
        Page<T> newPage = new Page(rawPage.getCurrent(), rawPage.getSize(), rawPage.getTotal());
        newPage.setRecords(rawPage.getRecords());
        return newPage;
    }

    public static <T> Page<T> newPage(Page rawPage, List<T> list) {
        Page<T> newPage = new Page(rawPage.getCurrent(), rawPage.getSize(), rawPage.getTotal());
        newPage.setRecords(list);
        return newPage;
    }

    public static <T> Page<T> newPage(IPage rawPage, List<T> list) {
        Page<T> newPage = new Page(rawPage.getCurrent(), rawPage.getSize(), rawPage.getTotal());
        newPage.setRecords(list);
        return newPage;
    }

    public static <T> Page<T> newPage(IPage rawPage, Class<T> clazz) {
        List records = rawPage.getRecords();
        return CollectionUtils.isEmpty(records) ? new Page(rawPage.getCurrent(), rawPage.getSize(), rawPage.getTotal()) : newPage(rawPage, BeanUtils.copyListProperties(records, clazz));
    }

    public static <T> Page<T> newPage(Page rawPage, Class<T> clazz) {
        List records = rawPage.getRecords();
        return CollectionUtils.isEmpty(records) ? new Page(rawPage.getCurrent(), rawPage.getSize(), rawPage.getTotal()) : newPage(rawPage, BeanUtils.copyListProperties(records, clazz));
    }

    public static <T> Page<T> newDeepPage(IPage rawPage, Class<T> clazz) {
        List records = rawPage.getRecords();
        return CollectionUtils.isEmpty(records) ? new Page(rawPage.getCurrent(), rawPage.getSize(), rawPage.getTotal()) : newPage(rawPage, BeanUtils.deepCopyListProperties(records, clazz));
    }

    public static <T> Page<T> newDeepPage(Page rawPage, Class<T> clazz) {
        List records = rawPage.getRecords();
        return CollectionUtils.isEmpty(records) ? new Page(rawPage.getCurrent(), rawPage.getSize(), rawPage.getTotal()) : newPage(rawPage, BeanUtils.deepCopyListProperties(records, clazz));
    }

    public static Page emptyPage(long current, long size) {
        return new Page(current, size, 0L);
    }

    public static Page emptyPage() {
        return new Page(1L, 10L, 0L);
    }
}
